package com.atguigu.guli.service.edu.controller.admin;


import com.atguigu.guli.service.base.result.R;
import com.atguigu.guli.service.edu.entity.Teacher;
import com.atguigu.guli.service.edu.entity.query.TeacherQuery;
import com.atguigu.guli.service.edu.feign.OssFeignClients;
import com.atguigu.guli.service.edu.mapper.TeacherMapper;
import com.atguigu.guli.service.edu.service.TeacherService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2021-06-24
 */
@Slf4j
@Api(tags = "讲师管理模块")
@RestController
@CrossOrigin  //解决异步请求跨域问题
@RequestMapping("/admin/edu/teacher")
public class AdminTeacherController {

    @Autowired
    private TeacherService teacherService;

    // 装配远程访问OssFeignClients的接口
    @Autowired
    private OssFeignClients ossFeignClients;


    //查询所有教师
    @ApiOperation(value = "查询所有教师")
    @GetMapping("getAll")
    public R getTeacherListAll() {

        log.debug("{}级别的日志{}", "debug", new Date());
        log.info("{}级别的日志{}", "info", new Date());
        log.warn("{}级别的日志{}", "warn", new Date());
        log.error("{}级别的日志{}", "error", new Date());

        try {
            TimeUnit.SECONDS.sleep(2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        List<Teacher> list = teacherService.list();
        return R.ok().data("items", list);
    }

    //新增讲师
    @ApiOperation(value = "新增讲师")
    @PostMapping("save")
    public R save(@ApiParam(name = "teacher", value = "新增讲师对象") @RequestBody Teacher teacher) {
        teacherService.save(teacher);
        return R.ok().message("添加讲师成功!");
    }

    //根据id删除教师
    @ApiOperation(value = "根据id删除教师")
    @DeleteMapping("del/{id}")
    public R deletedTeacher(@ApiParam(name="id", value = "用户id",defaultValue="1",required = true)
                                      @PathVariable("id") String id) {
        // 查询讲师信息, 获取讲师头像地址
        Teacher teacher = teacherService.getById(id);
        String avatarPath = teacher.getAvatar();

        // 删除讲师
        boolean b = teacherService.removeById(id);

        if(b) {
            // 如果讲师删除成功, 删除Oss中的头像照片
            if (!StringUtils.isEmpty(avatarPath)) {
                // 如果头像存在, 执行远程调用Oss服务删除图片
                ossFeignClients.delFile(avatarPath, "avatar");
            }

            return R.ok().message("删除成功!");
        } else {
            return R.error().message("删除失败!");
        }
    }

    //批量删除讲师
    @ApiOperation(value = "批量删除讲师")
    @DeleteMapping("delList")
    //public R deleteTeacherList(@RequestParam("ids")List<String> ids) {
    //public R deleteTeacherList(@RequestParam("ids") String[] ids) {
    public R deleteTeacherList(@RequestBody List<String> ids) {
        //System.out.println(ids);
        teacherService.removeByIds(ids);
        return R.ok();
    }

    //修改讲师信息
    @ApiOperation(value = "修改讲师信息")
    @PutMapping("update")
    public R updateTeacher(@RequestBody Teacher teacher) {
        teacherService.updateById(teacher);
        return R.ok();
    }

    //根据id查询讲师信息
    @GetMapping("getById/{id}")
    public R teacherById(@PathVariable String id) {
        Teacher teacher = teacherService.getById(id);
        return R.ok().data("item", teacher);
    }


    //分页查询
    @ApiOperation(value = "分页查询")
    @GetMapping("page/{pageNum}/{pageSize}")
    public R pageList(@PathVariable("pageNum") Integer pageNum,
                      @PathVariable("pageSize") Integer pageSize,
                      TeacherQuery queryObj) {
        Page<Teacher> page = new Page<>(pageNum, pageSize);
        page = teacherService.getPageByQuery(page, queryObj);

        return R.ok().data("page", page);
    }
}

